//
//  Sprite.mm
//  Prism
//
//  Created by Bas du Pre on 20-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "global.h"
#if PRISM_TARGET_SDL
#include "sdl/sdl_global.h"

#include "graphics/Sprite.h"
#include "graphics/Director.h"

using namespace Prism;
using namespace Prism::Graphics;

void Sprite::native_init() {
    
}

float Sprite::native_aliasfix(float var, bool aorb) {
    if (_textureFrame->texture()->antiAlias() == true) return var;
    
    float sign = var < 0 ? -1 : 1;
    var = var * sign; // abs(var)
    
    if ((int)var != var) {
        if (aorb) {
            var = (int)var + 1;
        } else {
            var = (int)var;
        }
    }
    
    return var * sign;
}

void Sprite::native_visit() {
    PointF pos = this->screenPosition();
    glTranslatef(pos.x, pos.y, 0.0);
    //glTranslatef(200, 200, 0);
    glRotatef(this->screenRotation(), 0.0, 0.0, 1.0);
    // Bind the texture to which subsequent calls refer to
    
    PointF ap = anchorPoint() * PointF(2.0, -2.0);
    ap -= PointF(1.0, -1.0);
    Size s = _textureFrame->texture()->native_size();
    
    Rect rect = _textureFrame->rect();
    RectF rectf;
    rectf.size.width = float(rect.size.width) / float(s.width);
    rectf.size.height = float(rect.size.height) / float(s.height);
    rectf.origin.x = float(rect.origin.x) / float(s.width);
    rectf.origin.y = float(rect.origin.y) / float(s.height);
    
    PFloat scaleX = this->screenScaleX();
    PFloat scaleY = this->screenScaleY();
    PFloat width05 = ((PFloat)rect.size.width) / 2.0 * scaleX;
    PFloat height05 = ((PFloat)rect.size.height) / 2.0 * scaleY;
    
    glBindTexture(GL_TEXTURE_2D, _textureFrame->texture()->glTexture);
    glBegin(GL_QUADS);
    
    ///\todo: als de height oneven is (height05 is x.5, dan wordt er niet mooi weergegeven
    
    // Top-left vertex (corner)
    glTexCoord2f(rectf.origin.x, rectf.origin.y);
    glVertex3f(native_aliasfix(-width05 - width05 * ap.x, true), native_aliasfix(-height05 + height05 * ap.y, true), 0);
    //PRISM_LOG("TL: %f, %f", -width05 - width05 * ap.x, -height05 + height05 * ap.y);
    // Top-right vertex (corner)
    glTexCoord2f(rectf.origin.x + rectf.size.width, rectf.origin.y);
    glVertex3f(native_aliasfix(width05 - width05 * ap.x, false), native_aliasfix(-height05 + height05 * ap.y, true), 0);
    //PRISM_LOG("TR: %f, %f", width05 - width05 * ap.x, -height05 + height05 * ap.y);
    // Bottom-right vertex (corner)
    glTexCoord2f(rectf.origin.x + rectf.size.width, rectf.origin.y + rectf.size.height);
    glVertex3f(native_aliasfix(width05 - width05 * ap.x, true), native_aliasfix(height05 + height05 * ap.y, false), 0);
    //PRISM_LOG("BR: %f, %f", width05 - width05 * ap.x, height05 + height05 * ap.y);
    // Bottom-left vertex (corner)
    glTexCoord2f(rectf.origin.x, rectf.origin.y + rectf.size.height);
    glVertex3f(native_aliasfix(-width05 - width05 * ap.x, false), native_aliasfix(height05 + height05 * ap.y, false), 0);
    //PRISM_LOG("BL: %f, %f", -width05 - width05 * ap.x, height05 + height05 * ap.y);
    
    glEnd();
    glLoadIdentity();
}

void Sprite::native_loadSprite() {
    /*
     //IOS_sprite = [CCSprite spriteWithTexture:_textureFrame->texture()->native_toCCTexture2D() rect:native_prismRectToCGRect(_textureFrame->rect())];
    // native_toCCTexture2D fixes @2x automagically
    _textureFrame->texture()->native_toCCTexture2D();
    
    NSString* filename = [NSString stringWithUTF8String: _textureFrame->texture()->file()->filename()]; 
    NSString* ext = [filename pathExtension];
    NSString* p = [filename stringByDeletingPathExtension];
    NSString* hd = [NSString stringWithFormat:@"%@-hd.%@", p, ext];
    if (!Prism::File::exists([hd cStringUsingEncoding:[NSString defaultCStringEncoding]]))
    {
        if ([[CCDirector sharedDirector] contentScaleFactor] == 2.0) {
            if ([p hasSuffix: @"@2x"]) {
                native_scale = 1;       
            }
        }
    } else {
        native_scale = 1;
    }
    IOS_sprite = [CCSprite node];
    native_setCCNode(IOS_sprite);
    native_updateSprite();
     */
}

void Sprite::native_updateSprite() {
    /*CCTexture2D* newTexture = _textureFrame->texture()->native_toCCTexture2D();
    if ([IOS_sprite texture] != newTexture) {
        [IOS_sprite setTexture:newTexture];
    }
    
    if (native_isRetinaNode()) {
        [IOS_sprite setTextureRect:native_prismRectToCGRect(_textureFrame->rect())];
    } else { 
        CGRect r = native_prismRectToCGRect(_textureFrame->rect());
        [IOS_sprite setTextureRectInPixels:r rotated:NO untrimmedSize:r.size];
    }
     */
    
    // TODO: update nativeScale if this is a @2x or not anymore
}

Prism::Rect Sprite::native_boundingBox() {
    Prism::Rect rect;
    /*
    CGRect ccRect = [IOS_sprite boundingBox];
    
    rect.origin.x = ccRect.origin.x;
    rect.origin.y = ccRect.origin.y;
    rect.size.width = ccRect.size.width;
    rect.size.height = ccRect.size.height;
    
    rect.origin = native_correctPosition(rect.origin);
    rect.origin.y -= rect.size.height;
    */
    return rect;
}


#endif
